package zhengyanze.com.aipoemmax.guide.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.adapter.LeiXingAdapter;
import zhengyanze.com.aipoemmax.guide.bean.LeiXingBean;
import zhengyanze.com.aipoemmax.guide.fragment.filter.MingJuFragment;
import zhengyanze.com.aipoemmax.guide.utils.TypefaceUtil;

import static zhengyanze.com.aipoemmax.guide.activity.PoemDetilsActivity.MINGJU_KEY;

public class MingActivity extends AppCompatActivity {
    public static final String LEIXING_KEY = "leixing.key";
    public static final String TITLE_KEY = "title.key";
    private static final String TAG = "MingActivity";
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_ming)

    RecyclerView rvMing;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    ArrayList<LeiXingBean> mLeiBeans;
    private LeiXingAdapter mLeiAdapter;
    private String moreLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ming);
        ButterKnife.bind(this);
        mLeiBeans = new ArrayList<>();
        LeiXingBean leinBean = getIntent().getParcelableExtra(LEIXING_KEY);
        String url = leinBean.getLink();

        initView();
        initData(url);
    }


    public void initView() {
        String title = getIntent().getStringExtra(TITLE_KEY);
        setSupportActionBar(toolbar);
        setTitle(title);
        toolbar.setNavigationIcon(R.mipmap.title_back_normal);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        rvMing.setLayoutManager(new LinearLayoutManager(this));
        mLeiAdapter = new LeiXingAdapter(mLeiBeans) {
            @Override
            public void onclickEvent(LeiViewHolder holder, int position, final LeiXingBean bean) {
                TypefaceUtil util = new TypefaceUtil(MingActivity.this, Constant.TTF.TTF_MAIN_TYPE);
                util.setTypeface(holder.tv_leixing, false);
                holder.tv_leixing.setText(bean.getContent());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(MingActivity.this, PoemDetilsActivity.class);
                        intent.putExtra(MINGJU_KEY, bean);
                        startActivity(intent);
                    }
                });
            }
        };
        rvMing.setAdapter(mLeiAdapter);
        rvMing.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        //加载更多
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                if (moreLink != null) {
                    String url = Constant.URL.GUSHI_MINGJU + moreLink;
                    initData(url);
                }
            }
        });
    }

    public void initData(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect(url).get();
                    Element main3 = document.getElementsByClass("main3").first();
                    Element left = main3.getElementsByClass("left").first();
                    Element sons = left.getElementsByClass("sons").first();
                    Elements cont = sons.getElementsByClass("cont");

                    ArrayList<LeiXingBean> tempArray = new ArrayList<LeiXingBean>();
                    for (Element e : cont) {
//                        Log.e(TAG, "内容是: " + e.text());

                        Element a = e.getElementsByTag("a").first();

//                        Log.e(TAG, "标题链接是: " + a.attr("href").toString());
                        String link = Constant.URL.GUSHI_BASE + a.attr("href").toString();

                        LeiXingBean leibean = new LeiXingBean(e.text(), link);
                        tempArray.add(leibean);
                    }
                    //获取加载更多链接
                    Element pages = left.getElementsByClass("pages").last();
                    Element linkA = pages.getElementsByTag("a").last();
//                    Log.e(TAG, "超联机: " + linkA.attr("href").toString());
                    if (linkA == null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MingActivity.this, "没有更多结果了", Toast.LENGTH_SHORT).show();
                            }
                        });
                        moreLink = null;
                    }else{
                        moreLink = linkA.attr("href").toString();
                    }

                    mLeiBeans.addAll(tempArray);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mLeiAdapter.notifyDataSetChanged();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
